//index.js
//获取应用实例
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    index: 1,
    num: 1,
    text: "全国",
    inputText: "",
    filter1: 1,
    filter2: 3,
    id: 0,
    page: 0,
    name: "",
    main: true,
    listContent: true,
    masterList: [],
    deal: false,
    good: false,
  },
  /**
   * 抢单事件函数
   */

  search: function () {
    // wx.redirectTo({
    //   url: '../search/search',
    // })
    wx.navigateBack({
      delta: 1,
    })
  },
  masterDetail: function (e) {
    var id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '../serverDetail/serverDetail?id=' + id,
    })
  },
  // 获取师傅信息
  request: function () {
    var that = this;
    // this.setData({
    //   page: 0
    // })
    console.log(this.data.name);
    console.log(this.data.id);
    var page = that.data.page;
    console.log(page);
    if (this.data.id == undefined && that.data.name == undefined) {

    } else if (that.data.name != null && that.data.name != undefined && that.data.name != "") {
      wx.request({
        url: 'https://www.xiaozheng8.com/receiveUser/findAll/name',
        method: "post",
        data: {
          name: that.data.name,
          page: page++,
          size: 10
        },
        success: function (res) {
          console.log(res);
          if (res.data.code == 200) {
            var content = that.data.masterList.concat(res.data.data)
            that.setData({
              masterList: content,
              page: page
            });
            that.setData({
              listContent: true
            });
            wx.hideLoading();
            if(that.data.masterList.length==0){
                wx.showToast({
                  title: '查询无果',
                  icon: "loading"
                })
            }
          } else {
            that.setData({
              listContent: false
            })
            console.log("!1111111111")
            wx.showLoading({
              title: '加载中',
            })
          }

        },
        fail: function (res) {
          that.setData({
            listContent: false
          })
          wx.showLoading({
            title: '加载中',
          })
        },
        complete(res) {
          if (res.statusCode == 500) {
            that.setData({
              listContent: false
            })
            wx.showLoading({
              title: '加载中',
            })
          }
        }
      })

    // }
    //  else if ((that.data.id != null && that.data.id != undefined && that.data.id != "") && (that.data.name == null || that.data.name == undefined || that.data.name == "")) {
    //   wx.request({
    //     url: 'https://www.xiaozheng8.com/receiveUser/findAll',
    //     method: "post",
    //     data: {
    //       introduceId: that.data.id,
    //       page: page++,
    //       size: 10
    //     },
    //     success: function (res) {
    //       console.log(res);
    //       var content = that.data.masterList.concat(res.data.data)
    //       that.setData({
    //         masterList: content,
    //         page: page
    //       })
    //       wx.hideLoading();
    //     }
    //   })
    } else {
      wx.request({
        url: 'https://www.xiaozheng8.com/receiveUser/findAll',
        method: "post",
        data: {
          receiveUserId: 1,
          page: page++,
          size: 10
        },
        success: function (res) {
          console.log(res);
          var content = that.data.masterList.concat(res.data.data)
          that.setData({
            masterList: content,
            page: page
          })
          wx.hideLoading();
          if (that.data.masterList.length == 0) {
            wx.showToast({
              title: '查询无果',
              icon: "loading"
            })
          }
          console.log(that.data.masterList);
        }

      })
    }

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    console.log(options);
 
    if (options.name == undefined && options.id == undefined) {

    } else {
      that.setData({
        id: options.id,
        name: options.name
      });
    };
    this.request();
    // this.request();
    // var that=this;
    // this.setData({
    //   id:options.id,
    //   name:options.name
    // });
    // if(this.data.id==null||this.data.id==undefined||this.data.id==""){
    //   wx.request({
    //     url: 'https://www.xiaozheng8.com/receiveUser/findAll',
    //     method: "post",
    //     data:{
    //       name:that.data.name
    //     },
    //     success:function(res){
    //       console.log(res);
    //         that.setData({
    //           masterList:res.data.data
    //         })
    //       }
    //   })

    // }else if(that.data.name==null|that.data.name==undefined||that.data.name==""){
    //   wx.request({
    //     url: 'https://www.xiaozheng8.com/receiveUser/findAll',
    //     method: "post",
    //     data: {
    //       introduceId: that.data.id
    //     },
    //     success: function (res) {
    //       console.log(res);
    //       that.setData({
    //         masterList: res.data.data
    //       })
    //     }
    //   })
    // }
    if (that.data.listContent.length > 0) {

    } else {
      wx.showLoading({
        title: '查询无果',
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  deal: function () {
    var that = this;
    wx.pageScrollTo({
      scrollTop: 0,
      success: function (res) {
        that.setData({
          scrollTop: 0
        })
      }
    })
    if (that.data.scrollTop == 0) {
      that.setData({
        main: true,
      })
    } else {
      that.setData({
        main: false
      })
      setTimeout(function () {
        that.setData({
          main: true
        })
      }, 500)
    }
    this.setData({
      deal: true,
      good: false
    })
    // this.setData({
    //   page: 0
    // })
    var page = 0;
    if (this.data.filter1 == 1) {

      wx.request({
        url: 'https://www.xiaozheng8.com/receiveUser/findAll/filter',
        method: "post",
        data: {
          filter: 2,
          page: page,
          size: 10
        },
        success: function (res) {
          console.log(res);
          that.setData({
            filter1: 2,
            masterList: res.data.data,
            page: page,
            filter2: 3,
          })
          wx.hideLoading();
        }
      })

    } else if (that.data.filter1 == 2) {
      wx.request({
        url: 'https://www.xiaozheng8.com/receiveUser/findAll/filter',
        method: "post",
        data: {
          filter: 1,
          page: page,
          size: 10
        },
        success(res) {
          that.setData({
            filter1: 1,
            masterList: res.data.data,
            page: page,
            filter2: 3,
          })
          wx.hideLoading();
        }
      })
    }
  },
  // 综合
  total: function () {
    var that = this;
    wx.pageScrollTo({
      scrollTop: 0,
      success: function (res) {
        that.setData({
          scrollTop: 0
        })
      }
    })
    if (that.data.scrollTop == 0) {
      that.setData({
        main: true,
      })
    } else {
      that.setData({
        main: false
      })
      setTimeout(function () {
        that.setData({
          main: true
        })
      }, 500)
    }
    that.setData({
      good: false,
      deal: false
    })
    // this.setData({
    //   page: 0
    // })
    var page = 0;
    wx.request({
      url: 'https://www.xiaozheng8.com/receiveUser/findAll',
      method: "post",
      data: {
        receiveUserId: 1,
        page: 0,
        size: 10
      },
      success: function (res) {
        console.log(res);
        that.setData({
          masterList: res.data.data,
          filter1: 1,
          filter2: 3,
          page: page
        })
        console.log(that.data.masterList);
      }

    })
  },
  // 获取滚动 距离
  onPageScroll: function (res) {
    this.setData({
      scrollTop:res.scrollTop
    })
  },
  // 好评率
  good: function () {
    var that = this;
  
    wx.pageScrollTo({
      scrollTop: 0,
      success:function(res){
        that.setData({
          scrollTop:0
        })
      }
    })
    if(that.data.scrollTop==0){
      that.setData({
        main:true,
      })
    }else{
      that.setData({
        main:false
      })
      setTimeout(function(){
        that.setData({
          main:true
        })
      },500)
    }
    that.setData({
      good: true,
      deal: false
    })
    // this.setData({
    //   page:0
    // })
    var page = 0;
    if (this.data.filter2 == 3) {

      wx.request({
        url: 'https://www.xiaozheng8.com/receiveUser/findAll/filter',
        method: "post",
        data: {
          filter: 4,
          page: page,
          size: 10
        },
        success: function (res) {
          console.log(res);
          that.setData({
            filter2: 4,
            page: page,
            filter1: 1,
            masterList: res.data.data
          })
        }
      })
    } else if (that.data.filter2 == 4) {
      // that.setData({
      //   good: true,
      //   deal: false
      // })
      wx.request({
        url: 'https://www.xiaozheng8.com/receiveUser/findAll/filter',
        method: "post",
        data: {
          filter: 3,
          page: page,
          size: 10
        },
        success: function (res) {
          that.setData({
            filter2: 3,
            filter1: 1,
            page: page,
            masterList: res.data.data
          })
          wx.hideLoading();
        }
      })
    }
  },
  //点击好评的触底事件
  goodBottom: function () {
    var that = this;
    that.setData({
      good: true,
      deal: false
    })
    var page = that.data.page + 1;
    if (this.data.filter2 == 4) {

      wx.request({
        url: 'https://www.xiaozheng8.com/receiveUser/findAll/filter',
        method: "post",
        data: {
          filter: 4,
          page: page++,
          size: 10
        },
        success: function (res) {
          console.log(res);
          var content = that.data.masterList.concat(res.data.data)
          that.setData({
            page: page++,
            masterList: content
          })
        }
      })
    } else if (that.data.filter2 == 3) {

      wx.request({
        url: 'https://www.xiaozheng8.com/receiveUser/findAll/filter',
        method: "post",
        data: {
          filter: 3,
          page: page++,
          size: 10
        },
        success: function (res) {
          var content = that.data.masterList.concat(res.data.data)
          that.setData({
            page: page,
            masterList: content
          })
          wx.hideLoading();
        }
      })
    }
  },
  //点击成交额的触点事件
  dealBottom: function () {
    var that = this;
    that.setData({
      good: false,
      deal: true
    })
    var page = that.data.page + 1;
    if (this.data.filter1 == 2) {

      wx.request({
        url: 'https://www.xiaozheng8.com/receiveUser/findAll/filter',
        method: "post",
        data: {
          filter: 2,
          page: page++,
          size: 10
        },
        success: function (res) {
          console.log(res);
          var content = that.data.masterList.concat(res.data.data)
          that.setData({
            masterList: content,
            page: page,
          })
          wx.hideLoading();
        }
      })

    } else if (that.data.filter1 == 1) {
      wx.request({
        url: 'https://www.xiaozheng8.com/receiveUser/findAll/filter',
        method: "post",
        data: {
          filter: 1,
          page: page,
          size: 10
        },
        success(res) {
          var content = that.data.masterList.concat(res.data.data)
          that.setData({
            masterList: content,
            page: page,
          })
          wx.hideLoading();
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // this.request();
    this.animation = wx.createAnimation();
    var that=this;
   
  },
  translate: function (e) {
    // console.log(e);
    this.setData({
      isRuleTrue: true,
      index: 0,
      // name: e.target.dataset.name
    })
    // this.animation.translate(0, -30).step()
    // this.setData({ animation: this.animation.export() })
  },

  success: function () {
    this.setData({
      isRuleTrue: false,
      index: 1
    })
    // this.animation.translate(0, 0).step()
    // this.setData({ animation: this.animation.export() })
  },

  changeBgc: function (e) {
    this.setData({
      num: e.target.dataset.num,
      //   isRuleTrue: false,
      index: 0,
    })
  },

  // sreach: function () {
  //   // wx.navigateTo({
  //   //   url: '../../utils/sreach/sreach',
  //   //   success: (result) => {

  //   //   },
  //   //   fail: () => {},
  //   //   complete: () => {}
  //   // });
  //   this.setData({
  //     isTrue: true,
  //   })
  // },
  cancel: function () {
    this.setData({
      isTrue: false,
    })
  },

  inputText: function (e) {
    let that = this;
    // console.log(e);
    that.setData({
      inputText: e.target.dataset.name
    })
    wx.navigateTo({
      url: '../../utils/sreach/sreach',
      success: (result) => {

      },
      fail: () => { },
      complete: () => { }
    });
  },

  text: function (e) {
    let that = this;
    // console.log(e);
    that.setData({
      text: e.target.dataset.text
    })
  },

  // screen: function() {
  //   wx.navigateTo({
  //     url: '../../utils/screen/screen',
  //   })
  // },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    console.log(that.data.good);
    console.log(that.data.deal);
    console.log(that.data.filter1);
    console.log(that.data.filter2);
    if (this.data.good == true) {
      that.goodBottom();
    } else if (this.data.deal == true) {
      that.dealBottom()
    } else {
      that.request();
    }

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})